package com.flame.shapeville.controller;

import com.flame.shapeville.bean.Course;
import com.flame.shapeville.bean.User;
import com.flame.shapeville.enums.SceneStyle;
import com.flame.shapeville.factory.BeanFactory;
import com.flame.shapeville.service.CourseService;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;


import java.util.List;

/**
 * Controller for the course selection screen.
 * <p>
 * Displays a list of available courses and allows the user to select one.
 *
 * @author Flame Dev Team
 * @version 1.0
 * @since 2025-05-08
 */
public class CourseController implements JfController {

    /**
     * Label displaying the username.
     */
    @FXML
    private Label username;

    /**
     * Grid pane displaying course buttons.
     */
    @FXML
    private GridPane coursePane;

    /**
     * Parent node for the score panel.
     */
    @FXML
    private Parent score;

    /**
     * Controller for the score panel.
     */
    @FXML
    private ScoreController scoreController;

    /**
     * List of available courses.
     */
    private List<Course> courses;

    /**
     * Initializes the controller with user and course data.
     */
    @Override
    public void init() {
        User user = (User) BeanFactory.getBean("user");
        username.setText(user.getName());
        courses = ((CourseService) BeanFactory.getBean("courseService")).loadCourse();
        for (int i = 0; i < courses.size(); i++) {
            Course course = courses.get(i);
            Button button = new Button(course.getName());
            button.getStyleClass().add("choice-button");
            button.setOnAction(event -> {
                BeanFactory.setBean("curCourse", course);
                SceneController.setSceneStyle(SceneStyle.MAIN);
            });
            coursePane.add(button, i % 2, i / 2);
        }
        scoreController.init();
    }

    /**
     * Shows the score panel.
     */
    @FXML
    private void showScore() {
        score.setVisible(true);
        score.setDisable(false);
    }
}
